<template>
    <div>
        <el-form :inline="true" :model="page" class="demo-form-inline">
            <el-form-item label="耳号">
                <el-select v-model="page.basicInforId" placeholder="请选择" style="width:190px" clearable>
                    <el-option v-for="item in BasicInformationList" :label="item.earNumber"
                        :value="item.basicInforId" />
                </el-select>
            </el-form-item>
            <el-form-item label="栋舍类型">
          <el-select v-model="page.changeHouseTypeId" placeholder="请选择" style="width:190px" clearable>
            <el-option v-for="item in ChangeHouseTypeList" :label="item.changeHouseTypeName" :value="item.changeHouseTypeId" />
          </el-select>
        </el-form-item>
        <el-form-item label="转出栋舍">
            <el-input v-model="page.turnBuilding" clearable />
        </el-form-item>
        <el-form-item label="转出栏位">
            <el-input v-model="page.turnColumn" clearable />
        </el-form-item>
        <br>
        <el-form-item label="转入栋舍">
            <el-select v-model="page.bedroomId" placeholder="请选择" style="width:190px" @change="handlefield()" clearable>
                <el-option v-for="item in bedroomList" :label="item.bedroomName"
                :value="item.bedroomId" />
            </el-select>
        </el-form-item>
        <el-form-item label="转入栏位">
            <el-select v-model="page.fieldId" placeholder="请选择" style="width:190px">
                    <el-option v-for="item in fieldList" :key="item.fieldId" :label="item.fieldName" :value="item.fieldId" />
                </el-select>
        </el-form-item>
            <el-form-item>
                <el-button type="danger" @click="loadData()">查询</el-button>
            </el-form-item>
            <el-form-item>
                <el-button @click="router.push('/QueryChangeHouse')">重置</el-button>
            </el-form-item>
        </el-form>
        <hr>
        <el-button type="danger" :icon="Plus" @click="router.push('/AddChangeHouse')">新建</el-button>
        <el-button type="danger" :icon="Delete" @click="RangeDel()">删除</el-button>

        <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="changeHouseId" label="序号" align="center" />
            <el-table-column prop="changeHouseDate" label="转舍日期" width="130" align="center">
                <template #default="scope">
                    {{ scope.row.changeHouseDate.replace('T', '').substring(0, 10) }}
                </template>
            </el-table-column>
            <el-table-column prop="earNumber" label="猪只耳号" align="center" />
            <el-table-column prop="changeHouseTypeName" label="栋舍类型" align="center" width="120"/>
            <el-table-column prop="turnBuilding" label="转出栋舍" align="center" />
            <el-table-column prop="turnColumn" label="转出栏位" align="center" />
            <el-table-column prop="bedroomName" label="转入栋舍" align="center" />
            <el-table-column prop="fieldName" label="转入栏位" align="center" />
            <el-table-column prop="varietyName" label="品种" align="center" width="100"/>
            <el-table-column prop="gender" label="性别" align="center" >
                <template #default="scope">
                <el-tag type="primary" v-if="scope.row.gender==1">公</el-tag>
                <el-tag type="danger" v-else>母</el-tag>
                </template>
      </el-table-column>
            <el-table-column prop="createdOwner" label="创建人" align="center" />
            <el-table-column prop="createdTime" label="创建日期" width="130" align="center">
                <template #default="scope">
                    {{ scope.row.createdTime.replace('T', '').substring(0, 10) }}
                </template>
            </el-table-column>
            <el-table-column prop="updateOwner" label="修改人" align="center" />
            <el-table-column prop="updateTime" label="修改日期" width="90" align="center" />
            <el-table-column prop="examineOwner" label="审核人" align="center" />
            <el-table-column prop="examineTime" label="审核日期" width="90" align="center" />
            <el-table-column label="操作" align="center" width="110">
                <template #default="scope">
                    <el-button type="primary" link>审核</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <table>
            <tr>
                <td>
                    <el-pagination v-model:current-page="page.pageIndex" v-model:page-size="page.pageSize"
                        :page-sizes="[5, 10, 15, 20]" :background="true" layout="total,slot, prev, pager,next"
                        :total="page.totalCount" @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        prev-text="上一页"
                        next-text="下一页">
                        ,每页显示{{ page.pageSize }},当前第{{ page.pageIndex }}/{{ page.pageCount }}
                        <el-button class="btn-prev" @click="handleCurrentChange(1)">首页</el-button>
                    </el-pagination>
                </td>
                <td>
                    <el-pagination v-model:current-page="page.pageIndex" v-model:page-size="page.pageSize"
                        :page-sizes="[5, 10, 15, 20]" :background="true" layout="slot,sizes,jumper"
                        :total="page.totalCount" @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        prev-text="上一页"
                        next-text="下一页">
                        <el-button class="btn-prev" @click="handleCurrentChange(page.pageCount)">尾页</el-button>
                    </el-pagination>
                </td>
            </tr>
        </table>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import axios from 'axios'
import { Plus, Delete } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'

const apiUrl = import.meta.env.VITE_APP_API_URL;
const apiBedinUrl = import.meta.env.VITE_APPRead_API_URL;

const router = useRouter();
onMounted(() => {
    loadData();
    GetBasicInformation();
    handleBedroom();//栋舍
    GetChangeHouseType();//猪只转舍类型
})

//分页查询字段
const page = reactive({
    basicInforId: '',
    changeHouseTypeId: '',
    bedroomId: '',
    fieldId: '',
    turnBuilding: '',
    turnColumn: '',
    pageIndex: 1,
    pageSize: 5,
    totalCount: 0,
    pageCount: 0
})

//猪只转舍列表
const tableData = ref([])
const loadData = () => {
    axios.post(apiBedinUrl+'/api/ChangeHouse/ShowChangeHouse', {
        basicInforId: page.basicInforId || 0,
        changeHouseTypeId: page.changeHouseTypeId || 0,
        bedroomId: page.bedroomId || 0,
        fieldId: page.fieldId||0,
        pageIndex: page.pageIndex,
        turnBuilding: page.turnBuilding,
        turnColumn: page.turnColumn,
        pageSize: page.pageSize
    })
        .then(res => {
            console.log("猪只转舍列表", res.data.data.list);
            tableData.value = res.data.data.list;
            page.totalCount = res.data.data.totalCount;
            page.pageCount = res.data.data.pageCount;
        })
}

//猪只转舍类型列表
const ChangeHouseTypeList = ref([]);
const GetChangeHouseType = () => {
    axios.post(apiBedinUrl+'/api/ChangeHouse/GetChangeHouseType', {})
        .then(res => {
            console.log("猪只转舍类型耳号", res.data.data);
            ChangeHouseTypeList.value = res.data.data;
        })
}

//猪只信息耳号列表
const BasicInformationList = ref([]);
const GetBasicInformation = () => {
    axios.post(apiBedinUrl+'/api/Infancies/GetBasicInformation', {})
        .then(res => {
            console.log("猪只信息耳号", res.data.data);
            BasicInformationList.value = res.data.data;
        })
}

//响应式数据 获取栋舍
const condition=reactive({
    pageIndex:1,
    pageSize:100,
    bedroomTypeId:0,
    bedroomName:'',
    personName:''
})
//获取栋舍
const bedroomList:any=ref([])
const handleBedroom=()=>{
    axios.post(apiUrl+'/api/BedRoom/ShowBedroom',{
        pageIndex: condition.pageIndex,
        pageSize: condition.pageSize,
        bedroomTypeId: condition.bedroomTypeId,
        bedroomName: condition.bedroomName,
        personName:condition.personName
    }).then(res=>{
        console.log("栋舍",res.data.data)
        bedroomList.value=res.data.data.list
    })
}

//获取栏位
const fieldList:any=ref([])
const handlefield=()=>{
    axios.post(apiUrl+'/api/BedRoom/GetBrdroomIdField',{
        bedroomId:page.bedroomId
    }).then(res=>{
        console.log("栏位",res.data.data);
        fieldList.value=res.data.data
    })
}

//分页
const handleSizeChange = (val: number) => {
    page.pageSize = val;
    loadData();
};
const handleCurrentChange = (val: number) => {
    page.pageIndex = val;
    loadData();
};
</script>
